<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header v-show="id" content="编辑" @back="$router.back()" />
      <el-page-header v-show="ids" content="月卡缴费" @back="$router.back()" />
      <el-page-header v-show="!id&&!ids" content="添加" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="cardForm" label-width="100px" :model="cardForm" :rules="rules11">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model.trim="cardForm.personName" :disabled="ids?true:false" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model.trim="cardForm.phoneNumber" :disabled="ids?true:false" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="cardForm.carNumber" :disabled="ids?true:false" />
            </el-form-item>
            <el-form-item label="企业联系人" prop="carBrand">
              <el-input v-model="cardForm.carBrand" :disabled="ids?true:false" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="feeForm" label-width="100px" :model="feeForm" :rules="rules">
            <el-form-item label="有效日期" prop="payTime">
              <el-date-picker
                v-model="feeForm.payTime"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod">
                <el-option
                  v-for="item in paymentMethodList"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="submit">{{ id?'编辑':'确定' }}</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { createCardApi, getCardDetailAPI, updateCardAPI, recharge } from '@/api/CardAdd'
export default {
  name: 'CardAddPage',
  data() {
    const validateBrand = (rule, value, callback) => {
      const reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('输入正确的信息'))
      }
    }

    return {
      feeForm: {
        payTime: [],
        paymentAmount: null,
        paymentMethod: null
      },
      rules: {
        payTime: [
          { required: true, message: '支付时间不能为空', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '支付金额不能为空', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '支付方式不能为空', trigger: 'change' }
        ]
      },
      cardForm: {
        personName: '',
        phoneNumber: '',
        carBrand: '',
        carNumber: ''
      },
      rules11: {
        personName: [
          { required: true, message: '车主姓名不能为空', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }

        ],
        carBrand: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '车辆品牌不能为空', trigger: 'blur' },
          { validator: validateBrand, trigger: 'blur' }
        ]
      },
      paymentMethodList: [
        {
          industryCode: 'Alipay',
          industryName: '支付宝'
        },
        {
          industryCode: 'WeChat',
          industryName: '微信'
        },
        {
          industryCode: 'Cash',
          industryName: '线下'
        }
      ]
    }
  },
  computed: {
    ids() {
      return this.$route.query.ids
    },
    id() {
      return this.$route.query.id
    }
  },
  created() {
    if (this.id || this.ids) {
      this.getList()
    }
  },
  methods: {
    // 回显
    async getList() {
      const { data: {
        paymentAmount,
        paymentMethod,
        carInfoId,
        personName,
        phoneNumber,
        carBrand,
        carNumber,
        rechargeId,
        cardStartDate,
        cardEndDate
      }} = await getCardDetailAPI(this.id || this.ids)
      // console.log(res)
      this.cardForm = {
        personName,
        phoneNumber,
        carBrand,
        carInfoId,
        carNumber
      }
      this.feeForm = {
        paymentAmount,
        paymentMethod,
        rechargeId,
        payTime: [cardStartDate,
          cardEndDate]
      }
    },
    // 表单重置
    reset() {
      // 重置车辆表单信息
      this.$refs.feeForm.resetFields()
      // 重置缴费表单信息
      this.$refs.cardForm.resetFields()
    },
    async submit() {
      await this.$refs.cardForm.validate()
      await this.$refs.feeForm.validate()
      const data = {
        ...this.feeForm, ...this.cardForm, cardStartDate: this.feeForm.payTime[0], cardEndDate: this.feeForm.payTime[1]
      }
      // 删除多余字段
      delete data.payTime
      console.log(data)
      if (this.id) {
        await updateCardAPI(data)
        this.$message.success('编辑成功')
      } else if (this.ids) {
        await recharge(data)
      } else {
        createCardApi(data)
        this.$message.success('增加成功')
      }
      this.$message.success(this.id ? '编辑成功' : '增加成功')
      this.$refs.feeForm.resetFields()
      this.$refs.cardForm.resetFields()
      this.$router.back()
    }
  }

}
</script>

  <style scoped lang="scss">
  .add-card {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      align-items: center;
      padding: 0 20px;
      height: 64px;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;

        .title {
          height: 60px;
          line-height: 60px;
          padding-left: 20px;
        }

        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;

          .el-form {
            display: flex;
            flex-wrap: wrap;

            .el-form-item {
              width: 50%;
            }
          }
        }
      }
      .preview{
        img{
          width: 100px;
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
  .color{
    color:'#c5bfbf'
  }
  </style>
